package com.shop.thread.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author Song Yinghui
 * @date 2019/12/7
 * @Version 1.0
 */
public class ThreadTest {

    private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

    /**
     * 单个线程的线程池
     * 延迟多少秒后以固定频率执行
     */
    public void test(){
        executorService.scheduleAtFixedRate(()->{
            System.out.println(Thread.currentThread().getName()+"--延迟定时任务----");
        },10,2, TimeUnit.SECONDS);

        executorService.scheduleAtFixedRate(()->{
            System.out.println(Thread.currentThread().getName()+"other work---");
        },2,1,TimeUnit.SECONDS);


    }

    /**
     * 延迟10s，只执行一次
     */
    public void test2(){
        executorService.schedule(()-> System.out.println(System.currentTimeMillis()),10*1000L,TimeUnit.MILLISECONDS);
    }

    public static void main(String[] args) {
        new ThreadTest().test2();
    }

}
